package com.ssg.poultry.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssg.poultry.service.SerialPortService;

public class BaseServlet extends HttpServlet {

	protected static final long serialVersionUID = 1L;
	
	protected static ThreadLocal<HttpServletRequest> threadReq = new ThreadLocal<>();
	protected static ThreadLocal<HttpServletResponse> threadRes = new ThreadLocal<>();
	protected static String prefix = "/WEB-INF/view/";
	protected static String suffix = ".jsp";
	
	protected SerialPortService serialPortService = new SerialPortService(115200, 1);
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		threadReq.set(req);
		threadRes.set(res);
		super.service(req, res);
	}
	
	protected void jsp(String jsp) {
		HttpServletRequest req = threadReq.get();
		HttpServletResponse res = threadRes.get();
		try {
			req.getRequestDispatcher(prefix+jsp+suffix).forward(req, res);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
